package com.dl.mood.ui;

import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.dl.mood.R;
import com.dl.mood.databinding.ActivityAddMoodBinding;
import com.dl.mood.model.MoodItem;
import com.dl.mood.viewmodel.MoodViewModel;

import java.util.Date;

public class AddMoodActivity extends BaseActivity {
    public static final String TAG = "AddMoodActivity";
    ActivityAddMoodBinding binding;
    boolean chooseIcon = false;
    MoodViewModel moodViewModel;


    MoodItem item;
    String sysTime;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        moodViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(MoodViewModel.class);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_add_mood);
        binding.happyIcon.setOnClickListener(v->{
            chooseIcon = true;
            item = new MoodItem(R.mipmap.happy_icon,"乐乐","春风得意马蹄疾❤人生得意须尽欢",getSysTime());
            setUIText(item);

        });
        binding.angryEm.setOnClickListener(v -> {
            chooseIcon = true;
            item = new MoodItem(R.mipmap.angry_icon,"怒怒","力拔山兮气盖世！火冒三丈比天高",getSysTime());
            setUIText(item);
        });
        binding.sadEm.setOnClickListener(v -> {
            chooseIcon = true;
            item = new MoodItem(R.mipmap.sad_icon,"忧忧","为伊消得人憔悴.难过无需多言",getSysTime());
            setUIText(item);
        });
        binding.awfulEm.setOnClickListener(v -> {
            chooseIcon = true;
            item = new MoodItem(R.mipmap.awful_icon,"厌厌","赶紧毁灭吧",getSysTime());
            setUIText(item);
        });



        binding.addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!chooseIcon) {
                    Toast.makeText(getApplicationContext(), "选择一个当前心情再添加吧！", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "添加成功！", Toast.LENGTH_SHORT).show();
                    moodViewModel.insertMoodItem(item);
                    finish();
            Log.d(TAG, "onCreate: "+item.toString());
                }
            }
        });

    }

    private String getSysTime() {
       return sysTime = DateFormat.format("yyyy-MM-dd HH:mm:ss", new Date()).toString();
    }

    private void setUIText(MoodItem item) {
        binding.emTag.setText(item.getTitle());
        binding.emContent.setText(item.getContent());
        binding.chooseFlag.setText("当前选择" + item.getTitle());
    }


}